import Vue from 'vue'
import App from './App'
import router from './router'
import iView from 'iview'
import { CONFIG } from './config/index.js'
import Qs from 'qs'
import store from './store' 
import '@/assets/js/webim.config'
let WebIM = require('easemob-websdk')
Vue.prototype.$webim = WebIM
Vue.use(iView)


// 全局加载进度条
router.beforeEach((to, from, next) => {
    iView.LoadingBar.start()
    if (to.matched.some(record => record.meta.requiresAuth)) {
        //这里判断用户是否登录，验证本地存储是否有token
        if (!localStorage.token) { // 判断当前的token是否存在
            next({
                path: '/'
            })
        } else {
            next()
        }
    } else {
        next() // 确保一定要调用 next()
    }
})
router.afterEach(route => {
    window.scrollTo(0,0)
    iView.LoadingBar.finish()
})


if(localStorage.getItem('token')){
    store.commit('set_token',localStorage.getItem('token'))
}


const conn = new WebIM.connection({
    isMultiLoginSessions: WebIM.config.isMultiLoginSessions,
    https: typeof WebIM.config.https === 'boolean' ? WebIM.config.https : location.protocol === 'https:',
    url: WebIM.config.xmppURL,
    heartBeatWait: WebIM.config.heartBeatWait,
    autoReconnectNumMax: WebIM.config.autoReconnectNumMax,
    autoReconnectInterval: WebIM.config.autoReconnectInterval,
    apiUrl: WebIM.config.apiURL,
    isAutoLogin: true
})

const options = {
    apiUrl: WebIM.config.apiURL,
    user: '',
    pwd: '123456',
    appKey: WebIM.config.appkey,
    success:function (re) {
        console.log('链接服务器正常')
    },
    error:function (err) {
        console.log(err)
    }
}

Vue.prototype.$imconn = conn
Vue.prototype.$imoption = options

// 引入静态样式
require('./assets/css/reset.css')
require('./assets/css/layout.css')
require('./assets/css/top3.css')
require('./assets/css/footer.css')
require('./assets/css/home.css')
require('./assets/css/accompanyPlay.css')
require('./assets/css/page.css')
require('./assets/css/announcement.css')
require('./assets/css/top2.css')
require('./assets/css/RequestService.css')
require('./assets/css/recharge.css')
require('./assets/css/left-menu.css')
require('./assets/css/news.css')
require('./assets/css/Login.css')
require('./assets/css/top.css')
require('./assets/css/detail.css')
require('./assets/css/center.css')

Vue.config.productionTip = false
Vue.prototype.$qs = Qs
Vue.use(CONFIG)

new Vue({
    el: '#app',
    router,
    store,
    components: { App },
    template: '<App/>'
})